package bd403.Backend;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/* Representation of a Wine. Implements the Drink interface.
 * Main author: Joe
 */

public class Wine implements Drink {
	private String name;
	private List<Review> reviews;
	
	/* Creates a new Wine with name n.
	 */
	public Wine(String n) {
		name = n;
		reviews = new ArrayList<Review>();
	}

	/* Returns a String representation of the name of this Wine.
	 */
	public String getName() {
		return name;
	}
	
	/* Returns an integer identifier for a Wine.
	 */
	public int getType() {
		return 1;
	}

	/* Returns an Iterator of reviews for this Wine.
	 */
	public ListIterator<Review> getReviews() {
		return reviews.listIterator(reviews.size());
	}

	/* Returns a double representing the rating of this Wine.
	 */
	public double getRating() {
		// The rating is the average rating for this wine. It is calculated by dividing
		// the total rating by the total number of reviews. Once a Wine is constructed,
		// it should always have at least one review. If not, return -1.0 for rating
		// to indicate there is an error.
		if (reviews.size() == 0) {
			return -1.0;
		} else {
			double sum = 0;
			for (Review r : reviews) {
				sum += r.getRating();
			}
			return sum / reviews.size();
		}
	}

	/* Adds a review for this Wine.
	 */
	public void addRating(Review r) {
		reviews.add(r);
	}
	
	/* Returns the total number of reviews for this Wine.
	 */
	public int getNumRatings() {
		return reviews.size();
	}
}	
